package com.iotechn.unimall.admin.util;

import com.iotechn.unimall.data.domain.OrderSkuDO;
import com.iotechn.unimall.data.dto.order.OrderDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by haha on 2017/4/25.
 */
public class EscPosUtil {
    private static final Logger logger = LoggerFactory.getLogger(EscPosUtil.class);

        public static final byte ESC = 27;// 换码
        public static final byte FS = 28;// 文本分隔符
        public static final byte GS = 29;// 组分隔符
        public static final byte DLE = 16;// 数据连接换码
        public static final byte EOT = 4;// 传输结束
        public static final byte ENQ = 5;// 询问字符
        public static final byte SP = 32;// 空格
        public static final byte HT = 9;// 横向列表
        public static final byte LF = 10;// 打印并换行（水平定位）
        public static final byte CR = 13;// 归位键
        public static final byte FF = 12;// 走纸控制（打印并回到标准模式（在页模式下） ）
        public static final byte CAN = 24;// 作废（页模式下取消打印数据 ）


    /**
     * BarCode table
     */
    public static class BarCode {
        public static final byte UPC_A       = 0;
        public static final byte UPC_E       = 1;
        public static final byte EAN13       = 2;
        public static final byte EAN8        = 3;
        public static final byte CODE39      = 4;
        public static final byte ITF         = 5;
        public static final byte NW7         = 6;
        //public static final byte CODE93      = 72;
        public static final byte CODE128     = 73;
    }

        /**
         * 打印机初始化
         *
         * @return
         */
        public static byte[] init_printer() {
            byte[] result = new byte[2];
            result[0] = ESC;
            result[1] = 64;
            return result;
        }

        // ------------------------换行-----------------------------

        /**
         * 换行
         *
         * @param -lineNum要换几行

         */
        public static byte[] nextLine(int lineNum) {
            byte[] result = new byte[lineNum];
            for (int i = 0; i < lineNum; i++) {
                result[i] = LF;
            }

            return result;
        }

        // ------------------------下划线-----------------------------

        /**
         * 绘制下划线（1点宽）

         */
        public static byte[] underlineWithOneDotWidthOn() {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 45;
            result[2] = 1;
            return result;
        }

        /**
         * 绘制下划线（2点宽）
         *
         */
        public static byte[] underlineWithTwoDotWidthOn() {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 45;
            result[2] = 2;
            return result;
        }

        /**
         * 取消绘制下划线
         */
        public static byte[] underlineOff() {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 45;
            result[2] = 0;
            return result;
        }


        // ------------------------加粗-----------------------------

        /**
         * 选择加粗模式
         */
        public static byte[] boldOn() {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 69;
            result[2] = 0xF;
            return result;
        }

        /**
         * 取消加粗模式
         */
        public static byte[] boldOff() {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 69;
            result[2] = 0;
            return result;
        }

        // ------------------------对齐-----------------------------

        /**
         * 左对齐
         */
        public static byte[] alignLeft() {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 97;
            result[2] = 0;
            return result;
        }

        /**
         * 居中对齐
         */
        public static byte[] alignCenter() {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 97;
            result[2] = 1;
            return result;
        }

        /**
         * 右对齐
         */
        public static byte[] alignRight() {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 97;
            result[2] = 2;
            return result;
        }

        /**
         * 水平方向向右移动col列
         */
        public static byte[] set_HT_position(byte col) {
            byte[] result = new byte[4];
            result[0] = ESC;
            result[1] = 68;
            result[2] = col;
            result[3] = 0;
            return result;
        }
        // ------------------------字体变大-----------------------------

        /**
         * 字体变大为标准的n倍
         */
        public static byte[] fontSizeSetBig(int num) {
            byte realSize = 0;
            switch (num) {
                case 1:
                    realSize = 0;
                    break;
                case 2:
                    realSize = 17;
                    break;
                case 3:
                    realSize = 34;
                    break;
                case 4:
                    realSize = 51;
                    break;
                case 5:
                    realSize = 68;
                    break;
                case 6:
                    realSize = 85;
                    break;
                case 7:
                    realSize = 102;
                    break;
                case 8:
                    realSize = 119;
                    break;
            }
            byte[] result = new byte[3];
            result[0] = 29;
            result[1] = 33;
            result[2] = realSize;
            return result;
        }

        /**
         * 字体取消倍宽倍高
         */
        public static byte[] fontSizeSetSmall(int num) {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 33;

            return result;
        }




        // ------------------------切纸-----------------------------

        /**
         * 进纸并全部切割
         *
         */
        public static byte[] feedPaperCutAll() {
            byte[] result = new byte[4];
            result[0] = GS;
            result[1] = 86;
            result[2] = 65;
            result[3] = 0;
            return result;
        }

        /**
         * 进纸并切割（左边留一点不切）
         */
        public static byte[] feedPaperCutPartial() {
            byte[] result = new byte[4];
            result[0] = GS;
            result[1] = 86;
            result[2] = 66;
            result[3] = 0;
            return result;
        }


    /**
     * Print and line feed
     * LF
     * @return bytes for this command
     */
    public static String printLinefeed()
    {
        return "$ESC_10";
    }

    /**
     * Turn underline mode on, set at 1-dot width
     * ESC - n
     * @return bytes for this command
     */
    public static String underline1DotOn()
    {
        return "$ESC_27_45_1";
    }

    /**
     * Turn underline mode on, set at 2-dot width
     * ESC - n
     * @return bytes for this command
     */
    public static String underline2DotOn()
    {
        return "$ESC_27_45_2";
    }



    /**
     * Initialize printer
     * Clears the data in the print buffer and resets the printer modes to the modes that were
     * in effect when the power was turned on.
     * ESC @
     * @return bytes for this command
     */
    public static String initPrinter()
    {
        return "$ESC_27_64";
    }

    /**
     * Turn emphasized mode on
     * ESC E n
     * @return bytes for this command
     */
    public static String emphasizedOn()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 69;
        result[2] = 0xF;
        return "$ESC_27_69_1";
    }

    /**
     * Turn emphasized mode off
     * ESC E n
     * @return bytes for this command
     */
    public static String emphasizedOff()
    {
        return "$ESC_27_69_0";
    }

    /**
     * double_strike_on
     * ESC G n
     * @return bytes for this command
     */
    public static String doubleStrikeOn()
    {
        return "$ESC_27_71_1";
    }

    /**
     * double_strike_off
     * ESC G n
     * @return bytes for this command
     */
    public static String doubleStrikeOff()
    {
        return "$ESC_27_71_0";
    }

    /**
     * Select Font A
     * ESC M n
     * @return bytes for this command
     */
    public static String selectFontA()
    {
        return "$ESC_27_77_0";
    }

    /**
     * Select Font B
     * ESC M n
     * @return bytes for this command
     */
    public static String selectFontB()
    {
        return "$ESC_27_77_1";
    }

    /**
     * Select Font C ( some printers don't have font C )
     * ESC M n
     * @return bytes for this command
     */
    public static String selectFontC()
    {
        return "$ESC_27_77_2";
    }

    /**
     * double height width mode on Font A
     * ESC ! n
     * @return bytes for this command
     */
//    public static String doubleHeightWidthOn()
//    {
//        return "$ESC_27_33_56";
//    }

    public static List<String> doubleHeightWidthOn()
    {
        return Arrays.asList("$ESC_27_33_56","$ESC_28_33_12");
    }

    /**
     * double height width mode off Font A
     * ESC ! n
     * @return bytes for this command
     */
    public static List<String> doubleHeightWidthOff()
    {
        return Arrays.asList("$ESC_27_33_0","$ESC_28_33_0");
    }

    /**
     * Select double height mode Font A
     * ESC ! n
     * @return bytes for this command
     */
    public static String doubleHeightOn()
    {
        return "$ESC_27_33_16";
    }

    /**
     * disable double height mode, select Font A
     * ESC ! n
     * @return bytes for this command
     */
    public static String doubleHeightOff()
    {
        return "$ESC_27_33_0";
    }

    /**
     * justification_left
     * ESC a n
     * @return bytes for this command
     */
    public static String justificationLeft()
    {
        return "$ESC_27_97_0";
    }

    /**
     * justification_center
     * ESC a n
     * @return bytes for this command
     */
    public static String justificationCenter()
    {
        return "$ESC_27_97_1";
    }

    /**
     * justification_right
     * ESC a n
     * @return bytes for this command
     */
    public static String justificationRight()
    {
        return "$ESC_27_97_2";
    }

    /**
     * Print and feed n lines
     * Prints the data in the print buffer and feeds n lines
     * ESC d n
     * @param n lines
     * @return bytes for this command
     */
    public static String printAndFeedLines(byte n)
    {
        return "$ESC_27_100_" + String.valueOf(n);
    }

    /**
     * Print and reverse feed n lines
     * Prints the data in the print buffer and feeds n lines in the reserve direction
     * ESC e n
     * @param n lines
     * @return bytes for this command
     */
    public static String printAndReverseFeedLines(byte n)
    {
        return "$ESC_27_101_" + String.valueOf(n);
    }

    /**
     * Drawer Kick
     * Drawer kick-out connector pin 2
     * ESC p m t1 t2
     * @return bytes for this command
     */
    public static String drawerKick()
    {
        return "$ESC_27_112_0_60_120";
    }

    /**
     * Select printing color1
     * ESC r n
     * @return bytes for this command
     */
    public static String selectColor1()
    {
        return "$ESC_27_114_0";
    }

    /**
     * Select printing color2
     * ESC r n
     * @return bytes for this command
     */
    public static String selectColor2()
    {
        return "$ESC_27_114_1";
    }

    /**
     * Select character code table
     * ESC t n
     * @param cp example:CodePage.WPC1252
     * @return bytes for this command
     */
    public static String selectCodeTab(byte cp)
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 116;
        result[2] = cp;
        return "$ESC_27_116_" + String.valueOf(cp);
    }

    /**
     * white printing mode on
     * Turn white/black reverse printing mode on
     * GS B n
     * @return bytes for this command
     */
    public static String whitePrintingOn()
    {
        return "$ESC_29_66_1";
    }

    /**
     * white printing mode off
     * Turn white/black reverse printing mode off
     * GS B n
     * @return bytes for this command
     */
    public static String whitePrintingOff()
    {
        return "$ESC_29_66_0";
    }

    /**
     * feed paper and cut
     * Feeds paper to ( cutting position + n x vertical motion unit )
     * and executes a full cut ( cuts the paper completely )
     * @return bytes for this command
     */
    public static String feedPaperCut()
    {
        return "$ESC_29_86_65_0";
    }


    /**
     * select bar code height
     * Select the height of the bar code as n dots
     * default dots = 162
     * @param dots ( heigth of the bar code )
     * @return bytes for this command
     */
    public static String barcodeHeight(byte dots)
    {
        return "$ESC_29_104_" + String.valueOf(dots);
    }

    /**
     * select font hri
     * Selects a font for the Human Readable Interpretation (HRI) characters when printing a barcode, using n as follows:
     * @param n
     *           Font
     *           0, 48 Font A
     *           1, 49 Font B
     * @return bytes for this command
     */
    public static String selectFontHri( byte n )
    {
        return "$ESC_29_102_" + String.valueOf(n);
    }

    /**
     * select position_hri
     * Selects the print position of Human Readable Interpretation (HRI) characters when printing a barcode, using n as follows:
     * @param n
     *           Print position
     *           0, 48 Not printed
     *           1, 49 Above the barcode
     *           2, 50 Below the barcode
     *           3, 51 Both above and below the barcode
     * @return bytes for this command
     */
    public static String selectPositionHri( byte n )
    {
        return "$ESC_29_72_" + String.valueOf(n);
    }

    /**
     * print bar code
     * @param barcode_typ( Barcode.CODE39, Barcode.EAN8 ,...)
     * @param barcode2print
     * @return bytes for this command
     */
    public static byte[] printBarcode(byte barcode_typ, String barcode2print )
    {
        byte[] barcodebytes = barcode2print.getBytes();
        byte[] result = new byte[3+barcodebytes.length+1];
        result[0] = GS;
        result[1] = 107;
        result[2] = barcode_typ;
        int idx = 3;

        for ( int i = 0; i < barcodebytes.length; i++ )
        {
            result[idx] = barcodebytes[i];
            idx++;
        }
        result[idx] = 0;

        return result;
    }

    public static String printBarcode39(String barcodeContent) {
        byte[] barcodeBytes = barcodeContent.getBytes();
        StringBuilder result = new StringBuilder("$ESC_29_107_4_");
        for (int i = 0; i < barcodeBytes.length; i++) {
            result.append(barcodeBytes[i]).append("_");
        }
        result.append("0");
        return result.toString();
    }


    /**
     * Set horizontal tab positions
     * @param col ( coulumn )
     * @return bytes for this command
     */
    public String setHTPosition( byte col )
    {
        return "$ESC_27_68_" + String.valueOf(col) + "_0";
    }

    /**
     * Print and line feed
     * LF
     * @return bytes for this command
     */
    public static byte[] print_linefeed()
    {
        byte[] result = new byte[1];
        result[0] = LF;
        return result;
    }

    /**
     * Turn underline mode on, set at 1-dot width
     * ESC - n
     * @return bytes for this command
     */
    public  static byte[] underline_1dot_on()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 45;
        result[2] = 1;
        return result;
    }

    /**
     * Turn underline mode on, set at 2-dot width
     * ESC - n
     * @return bytes for this command
     */
    public  static byte[] underline_2dot_on()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 45;
        result[2] = 2;
        return result;
    }

    /**
     * Turn underline mode off
     * ESC - n
     * @return bytes for this command
     */
    public  static byte[] underline_off()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 45;
        result[2] = 0;
        return result;
    }


    /**
     * Turn emphasized mode on
     * ESC E n
     * @return bytes for this command
     */
    public  static byte[] emphasized_on()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 69;
        result[2] = 0xF;
        return result;
    }

    /**
     * Turn emphasized mode off
     * ESC E n
     * @return bytes for this command
     */
    public  static byte[] emphasized_off()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 69;
        result[2] = 0;
        return result;
    }

    /**
     * double_strike_on
     * ESC G n
     * @return bytes for this command
     */
    public  static byte[] double_strike_on()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 71;
        result[2] = 0xF;
        return result;
    }

    /**
     * double_strike_off
     * ESC G n
     * @return bytes for this command
     */
    public  static byte[] double_strike_off()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 71;
        result[2] = 0xF;
        return result;
    }

    /**
     * Select Font A
     * ESC M n
     * @return bytes for this command
     */
    public  static byte[] select_fontA()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 77;
        result[2] = 0;
        return result;
    }

    /**
     * Select Font B
     * ESC M n
     * @return bytes for this command
     */
    public  static byte[] select_fontB()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 77;
        result[2] = 1;
        return result;
    }

    /**
     * Select Font C ( some printers don't have font C )
     * ESC M n
     * @return bytes for this command
     */
    public  static byte[] select_fontC()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 77;
        result[2] = 2;
        return result;
    }

    /**
     * double height width mode on Font A
     * ESC ! n
     * @return bytes for this command
     */
    public  static byte[] double_height_width_on()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 33;
        result[2] = 56;
        return result;
    }

    /**
     * double height width mode off Font A
     * ESC ! n
     * @return bytes for this command
     */
    public static  byte[] double_height_width_off()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 33;
        result[2] = 0;
        return result;
    }

    /**
     * Select double height mode Font A
     * ESC ! n
     * @return bytes for this command
     */
    public  static byte[] double_height_on()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 33;
        result[2] = 16;
        return result;
    }

    /**
     * disable double height mode, select Font A
     * ESC ! n
     * @return bytes for this command
     */
    public static  byte[] double_height_off()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 33;
        result[2] = 0;
        return result;
    }

    /**
     * justification_left
     * ESC a n
     * @return bytes for this command
     */
    public static  byte[] justification_left()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 97;
        result[2] = 0;
        return result;
    }

    /**
     * justification_center
     * ESC a n
     * @return bytes for this command
     */
    public  static byte[] justification_center()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 97;
        result[2] = 1;
        return result;
    }

    /**
     * justification_right
     * ESC a n
     * @return bytes for this command
     */
    public  static byte[] justification_right()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 97;
        result[2] = 2;
        return result;
    }

    /**
     * Print and feed n lines
     * Prints the data in the print buffer and feeds n lines
     * ESC d n
     * @param n lines
     * @return bytes for this command
     */
    public static byte[] print_and_feed_lines(byte n)
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 100;
        result[2] = n;
        return result;
    }

    /**
     * Print and reverse feed n lines
     * Prints the data in the print buffer and feeds n lines in the reserve direction
     * ESC e n
     * @param n lines
     * @return bytes for this command
     */
    public static byte[] print_and_reverse_feed_lines(byte n)
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 101;
        result[2] = n;
        return result;
    }

    /**
     * Drawer Kick
     * Drawer kick-out connector pin 2
     * ESC p m t1 t2
     * @return bytes for this command
     */
    public static byte[] drawer_kick()
    {
        byte[] result = new byte[5];
        result[0] = ESC;
        result[1] = 112;
        result[2] = 0;
        result[3] = 60;
        result[4] = 120;
        return result;
    }

    /**
     * Select printing color1
     * ESC r n
     * @return bytes for this command
     */
    public static byte[] select_color1()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 114;
        result[2] = 0;
        return result;
    }

    /**
     * Select printing color2
     * ESC r n
     * @return bytes for this command
     */
    public static byte[] select_color2()
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 114;
        result[2] = 1;
        return result;
    }

    /**
     * Select character code table
     * ESC t n
     * @param cp example:CodePage.WPC1252
     * @return bytes for this command
     */
    public static  byte[] select_code_tab(byte cp)
    {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 116;
        result[2] = cp;
        return result;
    }

    /**
     * white printing mode on
     * Turn white/black reverse printing mode on
     * GS B n
     * @return bytes for this command
     */
    public  static byte[] white_printing_on()
    {
        byte[] result = new byte[3];
        result[0] = GS;
        result[1] = 66;
        result[2] = (byte)128;
        return result;
    }

    /**
     * white printing mode off
     * Turn white/black reverse printing mode off
     * GS B n
     * @return bytes for this command
     */
    public  static byte[] white_printing_off()
    {
        byte[] result = new byte[3];
        result[0] = GS;
        result[1] = 66;
        result[2] = 0;
        return result;
    }

    /**
     * feed paper and cut
     * Feeds paper to ( cutting position + n x vertical motion unit )
     * and executes a full cut ( cuts the paper completely )
     * @return bytes for this command
     */
    public  static byte[] feedpapercut()
    {
        byte[] result = new byte[4];
        result[0] = GS;
        result[1] = 86;
        result[2] = 65;
        result[3] = 0;
        return result;
    }

    /**
     * feed paper and cut partial
     * Feeds paper to ( cutting position + n x vertical motion unit )
     * and executes a partial cut ( one point left uncut )
     * @return bytes for this command
     */
    public  static byte[] feedpapercut_partial()
    {
        byte[] result = new byte[4];
        result[0] = GS;
        result[1] = 86;
        result[2] = 66;
        result[3] = 0;
        return result;
    }

    /**
     * select bar code height
     * Select the height of the bar code as n dots
     * default dots = 162
     * @param dots ( heigth of the bar code )
     * @return bytes for this command
     */
    public  static byte[] barcode_height(byte dots)
    {
        byte[] result = new byte[3];
        result[0] = GS;
        result[1] = 104;
        result[2] = dots;
        return result;
    }

    /**
     * select font hri
     * Selects a font for the Human Readable Interpretation (HRI) characters when printing a barcode, using n as follows:
     * @param n
     *           Font
     *           0, 48 Font A
     *           1, 49 Font B
     * @return bytes for this command
     */
    public  static byte[] select_font_hri( byte n )
    {
        byte[] result = new byte[3];
        result[0] = GS;
        result[1] = 102;
        result[2] = n;
        return result;
    }

    /**
     * select position_hri
     * Selects the print position of Human Readable Interpretation (HRI) characters when printing a barcode, using n as follows:
     * @param n
     *           Print position
     *           0, 48 Not printed
     *           1, 49 Above the barcode
     *           2, 50 Below the barcode
     *           3, 51 Both above and below the barcode
     * @return bytes for this command
     */
    public  static byte[] select_position_hri( byte n )
    {
        byte[] result = new byte[3];
        result[0] = GS;
        result[1] = 72;
        result[2] = n;
        return result;
    }

    /**
     * print bar code
     * @param barcode_typ( Barcode.CODE39, Barcode.EAN8 ,...)
     * @param barcode2print
     * @return bytes for this command
     */
    public  static byte[] print_bar_code(byte barcode_typ, String barcode2print )
    {
        byte[] barcodebytes = barcode2print.getBytes();
        byte[] result = new byte[3+barcodebytes.length+1];
//        byte[] result = new byte[3+barcodebytes.length];
        result[0] = GS;
        result[1] = 107;
        result[2] = barcode_typ;
        int idx = 3;

        for ( int i = 0; i < barcodebytes.length; i++ )
        {
            result[idx] = barcodebytes[i];
            idx++;
        }
        result[idx] = 0;

        return result;
    }

    /**
     * print bar code
     * @param barcode_typ( Barcode.CODE39, Barcode.EAN8 ,...)
     * @param barcode2print
     * @return bytes for this command
     */
    public  static byte[] print_bar_code128(byte barcode_typ, String barcode2print )
    {
        byte[] barcodebytes = barcode2print.getBytes();
        byte[] result = new byte[4+barcodebytes.length];
//        byte[] result = new byte[3+barcodebytes.length];
        result[0] = GS;
        result[1] = 107;
        result[2] = barcode_typ;
        result[3] = (byte) barcodebytes.length;
        /*result[4] = 123;
        result[5] =66;*/
        int idx = 4;

        for ( int i = 0; i < barcodebytes.length; i++ )
        {
            result[idx] = barcodebytes[i];
            idx++;
        }
        //result[idx] = 0;

        return result;
    }

    public static byte[] set_print_area_width(int leftMargin, int rightMargin )
    {

        byte[] result = new byte[4];
//        byte[] result = new byte[3+barcodebytes.length];
        result[0] = GS;
        result[1] = 87;
        result[2] = (byte) leftMargin;
        result[3] = (byte) rightMargin;
        return result;
    }

    public  static byte[] set_bar_code_width(int n)
    {

        byte[] result = new byte[3];
        result[0] = GS;
        result[1] = 119;
        result[2] = (byte) n;
        return result;
    }




    public static byte[] set_chinese_on() {
        byte[] result = new byte[2];
        result[0] = FS;
        result[1] = 38;
        return result;
    }

    public static byte[] set_chinese_off() {
        byte[] result = new byte[2];
        result[0] = FS;
        result[1] = 46;
        return result;
    }

    public static byte[] set_chinese_super_on() {
        byte[] result = new byte[3];
        result[0] = FS;
        result[1] = 33;
        result[2] = 12;
        return result;
    }
    public static byte[] set_chinese_super_off() {
        byte[] result = new byte[3];
        result[0] = FS;
        result[1] = 33;
        result[2] = 0;
        return result;
    }

        public static String getOrderString(OrderDTO order,String storeName) throws IOException {
           //根据订单获取到用户所购买是线上还是门店 来判断客户电话
            logger.info("getOrderString>>>进来了");
            StringBuffer socketWriter = new StringBuffer();
            socketWriter.append(new String(init_printer()));
            socketWriter.append(new String(alignCenter(), "UTF-8"));
            socketWriter.append("\n");
            socketWriter.append("睢宁菜系购物清单\n");
            socketWriter.append(new String(justification_left(), "UTF-8"));
            socketWriter.append("订单号："+order.getOrderNo()+"\n");
            socketWriter.append("收银员：睢宁菜系\n");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            socketWriter.append("时间:"+format.format(new Date())+"\n");
            socketWriter.append("--------------------------\n");
            //socketWriter.append(new String(emphasized_on(), "UTF-8"));
            //socketWriter.append(new String(double_height_width_on(), "UTF-8"));
            socketWriter.append("商品名称      单价*数量   金额");
            socketWriter.append("\n");
            List<OrderSkuDO> skuList=order.getSkuList();
            int allCount=0;
            Double allmoney=0d;
            logger.info("skuList===到这里了");
            for(int i=0;i<skuList.size();i++){

                String goodsName = skuList.get(i).getSpuTitle().length()>6?skuList.get(i).getSpuTitle().substring(0, 6):skuList.get(i).getSpuTitle();
                int k=goodsName.length();
                if(k<6){
                    for(int j=0;j<6-k;j++){
                        goodsName+="";
                    }
                }
                if(skuList.get(i).getPriceMode()!=null && skuList.get(i).getPriceMode().intValue()==2){
                    Double allPrice=skuList.get(i).getPrice()* Double.parseDouble(skuList.get(i).getWeight()) / 100.0;
                    logger.info("1111======skuList.get(i).getPriceMode()===="+skuList.get(i).getPriceMode()+">>>>>>>>allPrice============="+allPrice);
                    BigDecimal b = new BigDecimal(allPrice);
                    allPrice = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    logger.info("2222=====skuList.get(i).getPriceMode()===="+skuList.get(i).getPriceMode()+">>>>>>>>allPrice============="+allPrice);
                    socketWriter.append(goodsName+"   "+skuList.get(i).getPrice()/100.0+"*"+skuList.get(i).getWeight()+"kg   "+allPrice);
                    allCount+=1;
                }else{
                    allCount+=skuList.get(i).getNum();
                    Double allPrice=skuList.get(i).getPrice()*skuList.get(i).getNum() / 100.0;
                    logger.info("333333=====skuList.get(i).getPriceMode()===="+skuList.get(i).getPriceMode()+">>>>>>>>allPrice============="+allPrice);
                    BigDecimal b = new BigDecimal(allPrice);
                    allPrice = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    logger.info("44444444=====skuList.get(i).getPriceMode()===="+skuList.get(i).getPriceMode()+">>>>>>>>allPrice============="+allPrice);
                    socketWriter.append(goodsName+"   "+skuList.get(i).getPrice()/100.0+"*"+skuList.get(i).getNum()+"    "+allPrice);
                }
                socketWriter.append("\n");

            }
            socketWriter.append("\n--------------------------\n");
            socketWriter.append(new String(justification_left(), "UTF-8"));
            socketWriter.append("数量："+allCount+"\n");
            socketWriter.append("商品总价："+order.getSkuTotalPrice() / 100.0+"\n");
            if(order.getCouponPrice()!=null && order.getCouponPrice()>0) {
                socketWriter.append("优惠券：" + order.getCouponPrice() / 100 + "\n");
            }
            if(order.getDeductionPrice()!=null && order.getDeductionPrice()>0) {
                socketWriter.append("余额抵扣：" + order.getDeductionPrice() / 100.0 + "\n");
            }
            if(order.getDiscount()!=null && order.getDiscount()>0) {
                socketWriter.append("折扣：" + order.getDiscount()+ "\n");
            }
            socketWriter.append("运费："+order.getFreightPrice() / 100 + "\n");
            socketWriter.append("现金总计："+order.getActualPrice() / 100.0 +"\n");
            String PAYMENT = (order.getPayChannel()!=null && order.getPayChannel().equals("WX")) ?"微信支付":"现金支付";
            socketWriter.append("支付方式："+PAYMENT+"\n");
            if(order.getConsignee()!=null) {
                socketWriter.append("会员：" + order.getConsignee() + "\n");
            }
            if(storeName!=null) {
                socketWriter.append("地址：" + order.getAddress() + "\n");
            }
            socketWriter.append("谢谢惠顾！期待您的再次光临\n");
            //socketWriter.append("客服电话(18000111765)");
            socketWriter.append("\n");
            socketWriter.append("\n");
            socketWriter.append("\n");
            socketWriter.append("\n");
            socketWriter.append("\n");
            socketWriter.append(nextLine(2));
            socketWriter.append(new String(feedpapercut(), "UTF-8"));
            return socketWriter.toString();
        }
}
